package cn.bytepath.systemdictionary.controller;

import cn.bytepath.systemdictionary.domain.SystemDictionaryDetail;
import cn.bytepath.systemdictionary.domain.SystemDictionaryType;
import cn.bytepath.systemdictionary.query.SystemDictionaryDetailQuery;
import cn.bytepath.systemdictionary.service.ISystemDictionaryDetailService;
import cn.bytepath.systemdictionary.service.ISystemDictionaryTypeService;
import cn.bytepath.util.PageInfo;
import cn.bytepath.util.PageResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 产品字典详细对应的控制层
 * @author Jack.C
 * @createDate 2022-02-12
 * @version 1.0
 */
@RestController
@RequestMapping("/systemdictionarydetail")
@Api(tags = "产品字典详细接口",description = "产品字典详细接口详细描述")
public class SystemDictionaryDetailController {
    
    /**
     * 注入产品字典详细业务层对象
     */
    @Autowired
    private ISystemDictionaryDetailService systemDictionaryDetailService;

    /**
     * 注入产品字典分类业务层对象
     */
    @Autowired
    private ISystemDictionaryTypeService systemDictionaryTypeService;

    @ApiOperation("查询所有SystemDictionaryDetail对象")
    @GetMapping
    public List<SystemDictionaryDetail> findAll() {
        return systemDictionaryDetailService.findAll();
    }

    @ApiOperation("查询所有SystemDictionaryType对象,用于新增时做下拉菜单")
    @GetMapping("/getalltype")
    public List<SystemDictionaryType> findAllType() {
        return systemDictionaryTypeService.findAll();
    }

    @ApiOperation("高级查询并分页")
    @PostMapping
    public PageInfo<SystemDictionaryDetail> pageQuery(@RequestBody SystemDictionaryDetailQuery query) {
        return systemDictionaryDetailService.findPageInfoByQuery(query);
    }

    @ApiOperation("根据id删除产品字典详细对象")
    @DeleteMapping("/{id}")
    public PageResult delete(@PathVariable("id") Long id) {

        try {
            systemDictionaryDetailService.delete(id);
            return PageResult.SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return PageResult.FAIL.setMsg(e.getMessage());
        }
    }

    @ApiOperation("根据id批量删除产品字典详细对象")
    @PatchMapping
    public PageResult deleteBatch(@RequestBody Long[] ids) {

        try {
            systemDictionaryDetailService.deleteBatch(ids);
            return PageResult.SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return PageResult.FAIL.setMsg(e.getMessage());
        }
    }

    @ApiOperation(value = "产品字典详细添加或修改",notes = "如果有id就是修改否则就是添加")
    @PutMapping
    public PageResult saveOrUpdate(@RequestBody SystemDictionaryDetail systemDictionaryDetail) {
        try {
            systemDictionaryDetailService.saveOrUpdate(systemDictionaryDetail);
            return PageResult.SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return PageResult.FAIL.setMsg(e.getMessage());
        }
    }
}
